import java.util.Comparator;
import java.util.PriorityQueue;

public class Sort {
    public static void insertSort(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if(tmp > arr[j]){
                    break;
                }else{
                    arr[j+1] = arr[j];
                }
            }
            arr[j+1] = tmp;
        }
    }
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[minIndex] > array[j]){
                    minIndex = j;
                }
            }
            int tmp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = tmp;
        }
    }
    public static void main(String[] args) {
        int[] num = {9,5,7,4};
        selectSort(num);
    }
}
class Bigger implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}
class Solution {
    public int[] smallestK(int[] arr, int k) {
        PriorityQueue<Integer> ans = new PriorityQueue<>(new Bigger());
        int[] ret = new int[k];
        if(arr.length == 0 || k <= 0){
            return ret;
        }
        for (int i = 0; i < k; i++) {
            ans.add(arr[i]);
        }
        for (int i = k; i < arr.length; i++) {
            if(arr[i] < ans.peek()){
                ans.poll();
                ans.add(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = ans.poll();
        }
        return ret;
    }
}